package practice;


import java.util.*;

public class Day17 {
    //给你一个链表，两两交换其中相邻的节点，并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题（即，只能进行节点交换）。
    //链接：https://leetcode.cn/problems/swap-nodes-in-pairs/description/
    static class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode swapPairs(ListNode head) {
        if(head == null) {
            return head;
        }
        ListNode cur = head;
        ListNode prev = null;
        ListNode curNext = cur.next;
        while(curNext != null) {
            cur.next = curNext.next;
            curNext.next  = cur;
            if(cur != head) {
                prev.next = curNext;
            }else {
                head = curNext;
            }
            prev = cur;
            cur = cur.next;
            if(cur == null) {
                break;
            }
            curNext = cur.next;
        }
        return head;
    }
    //翻转链表
    public void reorderList(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        //1.找到中间节点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //开始翻转
        ListNode newHead = new ListNode(0);
        ListNode curNext = slow.next;
        slow.next = null;
        while(curNext != null) {
            ListNode next = curNext.next;
            curNext.next = newHead.next;
            newHead.next = curNext;
            curNext = next;
        }
        ListNode cur1 = head;
        ListNode cur2 = newHead.next;
        ListNode ret = new ListNode(0);
        ListNode cur = ret;
        while(cur != null) {
            ret.next = cur1;
            ret = cur1;
            cur1 = cur1.next;
            if(cur2 == null) {
                break;
            }
            ret.next = cur2;
            ret = cur2;
            cur2 = cur2.next;
        }
    }
    //合并k个有序链表
    //法一：可能超时（O(n*k*k)）n：数组的长度，k：每个链表的长度
    private ListNode merge(ListNode head1,ListNode head2) {
        ListNode newNode = new ListNode(0);
        ListNode cur = newNode;
        while(head1 != null && head2 != null) {
            if(head1.val > head2.val) {
                cur.next = head2;
                head2 = head2.next;
            }else {
                cur.next = head1;
                head1 = head1.next;
            }
            cur = cur.next;
        }
        while(head1 != null) {
            cur.next = head1;
            head1 = head1.next;
            cur = cur.next;
        }
        while(head2 != null) {
            cur.next = head2;
            head2 = head2.next;
            cur = cur.next;
        }
        return newNode.next;
    }
    public ListNode mergeKLists1(ListNode[] lists) {
        if(lists == null) {
            return lists[0];
        }
        int len = lists.length;
        ListNode node = null;

        if(len > 1) {
            node = merge(lists[0],lists[1]);
        }
        for(int i = 2; i < len; i++) {
            node = merge(node,lists[i]);
        }
        if(len == 1) {
            return lists[0];
        }
        return node;
    }
    //法二：用优先级队列进行优化（O(N*logK*k)）
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null) {
            return lists[0];
        }
        //法二：用优先级队列进行优化
        ListNode newHead = new ListNode(0);
        ListNode cur = newHead;
        PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
            @Override
            public int compare(ListNode node1, ListNode node2) {
                return node1.val - node2.val;
            }
        });
        for(int i = 0; i < lists.length; i++) {
            if(lists[i] != null) {
                queue.offer(lists[i]);
            }
        }
        while(!queue.isEmpty()) {
            ListNode top = queue.poll();
            cur.next = top;
            cur = top;
            top = top.next;
            if(top != null) {
                queue.offer(top);
            }
            System.out.println(cur.val);
        }
        return newHead.next;
    }
    //法三：使用递归
    public ListNode mergeKLists3(ListNode[] lists) {
        if(lists == null || lists.length == 0) {
            return null;
        }
        return mergeChild(lists,0,lists.length-1);
    }
    private ListNode mergeChild(ListNode[] lists, int left, int right) {
        System.out.println(left + " " + right);

        if(left >= right) {
            return lists[left];
        }
        int mid = (left + right) / 2;
        ListNode leftTree = mergeChild(lists,left,mid);
        ListNode rightTree = mergeChild(lists,mid+1,right);
        return merge(leftTree,rightTree);
    }
    //k个一组翻转链表
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode cur = head;
        int n = 0;
        while (cur != null) {
            n++;
            cur = cur.next;
        }
        cur = head;
        ListNode newHead = new ListNode(0);
        ListNode tmp = newHead;
        for(int i = 0; i < n/k; i++) {
            ListNode prev = cur;
            for(int j = 0; j < k; j++) {
                ListNode nodeNext = cur.next;
                cur.next = tmp.next;
                tmp.next = cur;
                cur = nodeNext;
            }
            while(tmp.next != null) {
                tmp = tmp.next;
            }
        }
        while(cur != null) {
            tmp.next = cur;
            cur = cur.next;
            tmp = tmp.next;
        }
        return newHead.next;
    }
    public static boolean CheckPermutation(String s1, String s2) {
        Map<Character,Integer> map1 = new HashMap<>();
        Map<Character,Integer> map2 = new HashMap<>();
        if(s1.length() != s2.length()) {
            return false;
        }
        for(int i = 0; i < s1.length(); i++) {
            map1.put(s1.charAt(i),map1.getOrDefault(s1.charAt(i),0) + 1);
        }
        for(int i = 0; i < s2.length(); i++) {
            map2.put(s2.charAt(i),map2.getOrDefault(s2.charAt(i),0) + 1);
            if(map2.getOrDefault(s2.charAt(i),0) > map1.getOrDefault(s2.charAt(i),0)) {
                return false;
            }
        }
        return true;
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> list = new LinkedList<>();
        Map<String,List<String>> map = new HashMap<>();
        int len = strs.length;
        for(String s : strs) {
            char[] arr = s.toCharArray();
            Arrays.sort(arr);
            String str = new String(arr);
            if(!map.containsKey(str)) {
                map.put(str,new LinkedList<String>());
            }
            map.get(str).add(s);
        }
//        for(Map.Entry entry : map.entrySet()) {
//            list.add((List<String>) entry.getValue());
//        }
//        return list;
        return new LinkedList<>(map.values());
    }
    //非对称之美（找规律，不是常规方法）
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int n = s.length();
        boolean flag = false;
        for(int i = 1; i < n; i++) {
            if(s.charAt(i) != s.charAt(0)) {
                flag = true;
                break;
            }
        }
        if(flag) {//不是相同字母
            int left = 0;
            int right = n - 1;
            while(left < right) {
                if(s.charAt(left) == s.charAt(right)) {
                    right--;
                    left++;
                }else {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                System.out.println(n-1);
            }else {
                System.out.println(n);
            }
        }else{
            System.out.println(0);
        }
    }

}
